package com.aegis.demo;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author wuweixin
 *
 * @Version 1.0
 * @Descritube
 */
@RestController
@RequestMapping("/app")
@Slf4j
public class DemoController {


    /**
     * GET
     * @param body
     * @param age
     * @param name
     * @param request
     */
    @GetMapping("/demo1")
    public String demo1(
            @RequestBody(required = false) Map<String, Object> body,
            @RequestParam("age") String age,
            @RequestParam("name") String name,
            HttpServletRequest request) {
        log.info("demo1请求成功");
        log.info("请求query: {}", request.getQueryString());
        log.info("age {}", age);
        log.info("name {}", name);
        if (body != null) {
            log.info("请求体: {}", JSONUtil.toJsonStr(body));
        }
        return "success";
    }

    /**
     * 接口路径相同，但方法不同
     * @param body
     * @param age
     * @param name
     * @param request
     */
    @DeleteMapping("/demo1")
    public String demo2(
            @RequestBody(required = false) Map<String, Object> body,
            @RequestParam("age") String age,
            @RequestParam("name") String name,
            HttpServletRequest request) {
        log.info("demo1请求成功");
        log.info("请求query: {}", request.getQueryString());
        log.info("age {}", age);
        log.info("name {}", name);
        if (body != null) {
            log.info("请求体: {}", JSONUtil.toJsonStr(body));
        }
        return "success";
    }

    /**
     * POST
     * @param body
     * @param age
     * @param name
     * @param request
     */
    @PostMapping("/demo2")
    public String demo3(@RequestBody Map<String, Object> body,
                      @RequestParam("age") String age,
                      @RequestParam("name") String name,
                      HttpServletRequest request) {
        log.info("demo2请求成功");
        log.info("请求query: {}", request.getQueryString());
        log.info("age {}", age);
        log.info("name {}", name);
        log.info("请求体: {}", JSONUtil.toJsonStr(body));
        return "success";
    }


}
